/***
This do-file plots an example of a break in the raw series of small businesses
open series and the correction we perform in the data processing pipeline.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results/Small Business Revenue"

*-------------------------------------------------------------------------------
* Import intermediate file after adjustment from Womply pipeline
*-------------------------------------------------------------------------------

project, uses("${root}/data/dvc/Womply/Womply Single Normed Adjusted BY statefips naics.dta")
use "${root}/data/dvc/Womply/Womply Single Normed Adjusted BY statefips naics.dta", clear

gcollapse (mean) s_merchants, by(statefips date) 

* Center at 0 instead of 1 
replace s_merchants = (s_merchants - 1) * 100

rename s_merchants s_merchants_adj 

keep date statefips s_merchants_adj

tempfile adj
save `adj'

*-------------------------------------------------------------------------------
* Import intermediate file before adjustment from the Womply pipeline
*-------------------------------------------------------------------------------

project, uses("${root}/data/dvc/Womply/Womply No-Entry Panel All Years - State-level.dta")
use "${root}/data/dvc/Womply/Womply No-Entry Panel All Years - State-level.dta", clear

* Choose state 
local state = 9 // Connecticut

* Center at 0 instead of 1 
replace s_merchants = (s_merchants - 1) * 100

* Get drop in August 3rd
sum s_merchants if statefips == 9 & date == mdy(8, 3, 2020) 
local after `r(mean)'

sum s_merchants if statefips == 9 & date == mdy(7, 27, 2020) 
local before `r(mean)'

local x = `=round(`=abs(`after' - `before')', 1)'

* Merge
merge 1:1 statefips date using `adj', nogen

*-------------------------------------------------------------------------------
* Plot 
*-------------------------------------------------------------------------------

tw (line s_merchants date if statefips == 9 & inrange(date, mdy(1, 13, 2020), mdy(12, 31, 2020)) , lcolor(oi1%50) lpattern(dash))  ///
	(line s_merchants_adj date if statefips == 9 & inrange(date, mdy(1, 13, 2020), mdy(12, 31, 2020)), lcolor(oi1)) ///
	, ///
	xline(`=mdy(8, 3, 2020)', lcolor(gs8) lpattern(dash)) ///
	ylabel(0 "0%" -10 "-10%" -20 "-20%" -30 "-30%" -40 "-40%" -50 "-50%", nogrid) ///
	text(-5 `=mdy(8, 3, 2020)+10' "Small Businesses Open series" ///
								"in Connecticut experiences a" ///
								"`x'pp drop in the week ending" ///
								"in Aug 3, 2020" ///
							  , placement(east) justification(left) color(gs8) size(3)) ///
	xlabel(`=mdy(1, 1, 2020)' `""Jan""2020""' `=mdy(3, 1, 2020)' "Mar" `=mdy(5, 1, 2020)' "May" ///
			`=mdy(7, 1, 2020)' "Jul" `=mdy(9, 1, 2020)' "Sep" `=mdy(11, 1, 2020)' "Nov" `=mdy(1, 1, 2021)' `""Jan""2021""') ///
	xtitle("") ///
	ytitle("Change in Small Businesses Open (%)" "Relative to January 2020") ///
	legend(order(1 "Before Adjustments" 2 "Adjusted") size(small))
	
oi_graph_export "${root}/results/Small Business Revenue/Small Businesses Open sharp drop Connecticut", type(${fig_type}) 
